import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testsecret:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/secret/1_get_secret_list.yaml'))
    def test_get_secret_list(self, secretinfo):
        # 获取secret列表接口-正常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = base_url + secretinfo['request']['url'].replace('{namespace}', 'default')
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/secret/2_create_secret.yaml'))
    def test_create_secret(self, secretinfo):
        # 创建secret接口-正常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = base_url + secretinfo['request']['url'].replace('{namespace}', 'default')
        method = secretinfo['request']['method']
        data = secretinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        secretname = req.json()['metadata']['name']
        globals()['secretname'] = secretname
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/secret/3_get_specific_secret.yaml'))
    def test_get_specific_secret(self, secretinfo):
        # 获取指定secret接口-正常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = (base_url + secretinfo['request']['url']
               .replace('{namespace}', 'default').replace('{secretname}', globals()['secretname']))
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/secret/4_update_secret.yaml'))
    def test_update_secret(self, secretinfo):
        # 更新指定secret接口-正常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = (base_url + secretinfo['request']['url']
               .replace('{namespace}', 'default').replace('{secretname}', globals()['secretname']))
        method = secretinfo['request']['method']
        secretinfo['request']['data'] = globals()['updatereqbody']
        data = secretinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/secret/5_update_section_secret.yaml'))
    def test_update_section_secret(self, secretinfo):
        # 部分更新指定secret的接口-正常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = (base_url + secretinfo['request']['url']
               .replace('{namespace}', 'default').replace('{secretname}', globals()['secretname']))
        method = secretinfo['request']['method']
        data = secretinfo['request']['data']
        header = secretinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/secret/6_delete_specific_secret.yaml'))
    def test_delete_specific_secret(self, secretinfo):
        # 删除secret接口-正常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = (base_url + secretinfo['request']['url']
               .replace('{namespace}', 'default').replace('{secretname}', globals()['secretname']))
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/secret/1_get_secret_list_exception.yaml'))
    def test_get_secret_list_exception(self, secretinfo):
        # 获取secret列表接口-异常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = base_url + secretinfo['request']['url'].replace('{namespace}', 'default')
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/secret/2_create_secret_exception.yaml'))
    def test_create_secret_exception(self, secretinfo):
        # 创建secret接口-异常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = base_url + secretinfo['request']['url'].replace('{namespace}', 'default')
        method = secretinfo['request']['method']
        data = secretinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/secret/3_get_specific_secret_exception.yaml'))
    def test_get_specific_secret_exception(self, secretinfo):
        # 获取指定secret接口-异常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = (base_url + secretinfo['request']['url']
               .replace('{namespace}', 'default').replace('{secretname}', globals()['secretname']))
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/secret/4_update_secret_exception.yaml'))
    def test_update_secret_exception(self, secretinfo):
        # 更新指定secret接口-异常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = (base_url + secretinfo['request']['url']
               .replace('{namespace}', 'default').replace('{secretname}', globals()['secretname']))
        method = secretinfo['request']['method']
        secretinfo['request']['data'] = globals()['updatereqbody']
        data = secretinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/secret/5_update_section_secret_exception.yaml'))
    def test_update_section_secret_exception(self, secretinfo):
        # 部分更新指定secret的接口-异常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = (base_url + secretinfo['request']['url']
               .replace('{namespace}', 'default').replace('{secretname}', globals()['secretname']))
        method = secretinfo['request']['method']
        data = secretinfo['request']['data']
        header = secretinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.secret
    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/secret/6_delete_specific_secret_exception.yaml'))
    def test_delete_specific_secret_exception(self, secretinfo):
        # 删除secret接口-异常
        base_url = datas.baseUrl + Testsecret.url_unify
        url = (base_url + secretinfo['request']['url']
               .replace('{namespace}', 'default').replace('{secretname}', globals()['secretname']))
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']
